<?php
  //Web page use to calcul and display the countries statistics
  
  //Do the sum of totals users
  include_once('connect.php');
  
  //login to the database
  $db = connect();
  
  //collect the number of lines in the 'globe' table
  $req = "SELECT COUNT(*) AS nbrLine FROM country";
  $reply = $db->query($req);
  $datas = $reply->fetch();
  $nbrLine = $datas['nbrLine'];
  
  $usersTot = 0;
  
  for ($i = 1; $i <= $nbrLine; ++$i) {
      $req = $db->prepare('SELECT quantity FROM country WHERE id=?');
      $req->execute(array($i));
      $country = $req->fetch();
      $quantity_tempo = $country['quantity'];
      
      $usersTot += $quantity_tempo;
  }
  
  for ($i = 1; $i <= $nbrLine; ++$i) {
      $req = $db->prepare('SELECT quantity, name FROM country WHERE id=?');
      $req->execute(array($i));
      $country = $req->fetch();
      $quantity_tempo = $country['quantity'];
      $name = $country['name'];
      
      //compute the percentage for each country
      $percent = $quantity_tempo / $usersTot * 100;
      
      echo $name . ' = ' . $percent . "</br>";
  }
?>